﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
using System.Text;

namespace SyndicationService
{
    public class ContentService : IContentService
    {
        #region IContentService Members

        public Content GetContent(string id)
        {
            ContentStore db = new ContentStore(@"Data Source=.\App_Data\content.sdf;Persist Security Info=True;Password=cactusHC123");

            //var content = from c in db.ContentDatas
            //              where c.ContentId == Convert.ToInt32(id)
            //              select c;

            ContentData data = db.ContentDatas.SingleOrDefault(p => p.ContentId == Convert.ToInt32(id));

            if (data == null)
                return null;
            else
                return new Content { Id = data.ContentId, Title = data.ContentTitle, ContentType = data.ContentType.ContentTypeName, Body = data.ContentBody, LastUpdatedTime = new DateTimeOffset(data.ContentDate ?? DateTime.Now)};
        }

        public SyndicationFeedFormatter GetContentSyndicationFeed(string category, string format)
        {
            // Create a new Syndication Feed.
            SyndicationFeed feed = new SyndicationFeed("Feed Title", "A WCF Syndication Feed", null);
            List<SyndicationItem> items = new List<SyndicationItem>();

            // Create a new Syndication Item(s).
            SyndicationItem item = new SyndicationItem("An item", "Item content", null);
            items.Add(item);
            feed.Items = items;

            // Return ATOM or RSS based on query string
            string query = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["format"];
            SyndicationFeedFormatter formatter = null;
            if (query == "atom")
            {
                formatter = new Atom10FeedFormatter(feed);
            }
            else
            {
                formatter = new Rss20FeedFormatter(feed);
            }

            return formatter;
        }

        public SyndicationFeedFormatter GetContentSyndicationFeed(string category, DateTime startDate, DateTime endDate, string format)
        {
            throw new NotImplementedException();
        }

        public void AddContent(Content content)
        {
            throw new NotImplementedException();
        }

        public void DeleteContent(int Id)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
